sliderLineComponent.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "../../propertyChangedEvent";
  4. import { Tools } from 'babylonjs/Misc/tools';
  5. interface ISliderLineComponentProps {
  6. label: string;
  7. target?: any;
  8. propertyName?: string;
  9. minimum: number;
  10. maximum: number;
  11. step: number;
  12. directValue?: number;
  13. useEuler?: boolean;
  14. onChange?: (value: number) => void;
  15. onInput?: (value: number) => void;
  16. replaySourceReplacement?: string;
  17. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  18. decimalCount?: number;
  19. }
  20. export class SliderLineComponent extends React.Component<ISliderLineComponentProps, { value: number }> {
  21. private _localChange = false;
  22. constructor(props: ISliderLineComponentProps) {
  23. super(props);
  24. if (this.props.directValue !== undefined) {
  25. this.state = {
  26. value: this.props.directValue
  27. }
  28. } else {
  29. let value = this.props.target![this.props.propertyName!];
  30. if (value === undefined) {
  31. value = this.props.maximum;
  32. }
  33. this.state = { value: value };
  34. }
  35. }
  36. shouldComponentUpdate(nextProps: ISliderLineComponentProps, nextState: { value: number }) {
  37. if (nextProps.directValue !== undefined) {
  38. nextState.value = nextProps.directValue;
  39. return true;
  40. }
  41. let currentState = nextProps.target![nextProps.propertyName!];
  42. if (currentState === undefined) {
  43. currentState = nextProps.maximum;
  44. }
  45. if (currentState !== nextState.value || this._localChange) {
  46. nextState.value = currentState;
  47. this._localChange = false;
  48. return true;
  49. }
  50. return false;
  51. }
  52. onChange(newValueString: any) {
  53. this._localChange = true;
  54. let newValue = parseFloat(newValueString);
  55. if (this.props.useEuler) {
  56. newValue = Tools.ToRadians(newValue);
  57. }
  58. if (this.props.target) {
  59. if (this.props.onPropertyChangedObservable) {
  60. this.props.onPropertyChangedObservable.notifyObservers({
  61. object: this.props.replaySourceReplacement ?? this.props.target,
  62. property: this.props.propertyName!,
  63. value: newValue,
  64. initialValue: this.state.value
  65. });
  66. }
  67. this.props.target[this.props.propertyName!] = newValue;
  68. }
  69. if (this.props.onChange) {
  70. this.props.onChange(newValue);
  71. }
  72. this.setState({ value: newValue });
  73. }
  74. onInput(newValueString: any) {
  75. const newValue = parseFloat(newValueString);
  76. if (this.props.onInput) {
  77. this.props.onInput(newValue);
  78. }
  79. }
  80. prepareDataToRead(value: number) {
  81. if (this.props.useEuler) {
  82. return Tools.ToDegrees(value);
  83. }
  84. return value;
  85. }
  86. render() {
  87. let decimalCount = this.props.decimalCount !== undefined ? this.props.decimalCount : 2;
  88. return (
  89. <div className="sliderLine">
  90. <div className="label">
  91. {this.props.label}
  92. </div>
  93. <div className="slider">
  94. {this.state.value ? this.prepareDataToRead(this.state.value).toFixed(decimalCount) : "0"}&nbsp;<input className="range" type="range" step={this.props.step} min={this.prepareDataToRead(this.props.minimum)} max={this.prepareDataToRead(this.props.maximum)} value={this.prepareDataToRead(this.state.value)}
  95. onInput={evt => this.onInput((evt.target as HTMLInputElement).value)}
  96. onChange={evt => this.onChange(evt.target.value)} />
  97. </div>
  98. </div>
  99. );
  100. }
  101. }