animationGroupPropertyGridComponent.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import * as React from "react";
  2. import { Observable, AnimationGroup, Scene, Nullable, Observer } from "babylonjs";
  3. import { PropertyChangedEvent } from "../../../propertyChangedEvent";
  4. import { ButtonLineComponent } from "../../lines/buttonLineComponent";
  5. import { LineContainerComponent } from "../../lineContainerComponent";
  6. import { TextLineComponent } from "../../lines/textLineComponent";
  7. import { SliderLineComponent } from "../../lines/sliderLineComponent";
  8. import { LockObject } from "./lockObject";
  9. interface IAnimationGroupGridComponentProps {
  10. animationGroup: AnimationGroup,
  11. scene: Scene,
  12. lockObject: LockObject,
  13. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  14. }
  15. export class AnimationGroupGridComponent extends React.Component<IAnimationGroupGridComponentProps, { playButtonText: string, currentFrame: number }> {
  16. private _onAnimationGroupPlayObserver: Nullable<Observer<AnimationGroup>>;
  17. private _onAnimationGroupPauseObserver: Nullable<Observer<AnimationGroup>>;
  18. private _onBeforeRenderObserver: Nullable<Observer<Scene>>;
  19. constructor(props: IAnimationGroupGridComponentProps) {
  20. super(props);
  21. const animationGroup = this.props.animationGroup;
  22. this.state = { playButtonText: animationGroup.isPlaying ? "Pause" : "Play", currentFrame: 0 };
  23. }
  24. disconnect(animationGroup: AnimationGroup) {
  25. if (this._onAnimationGroupPlayObserver) {
  26. animationGroup.onAnimationGroupPlayObservable.remove(this._onAnimationGroupPlayObserver);
  27. this._onAnimationGroupPlayObserver = null;
  28. }
  29. if (this._onAnimationGroupPauseObserver) {
  30. animationGroup.onAnimationGroupPauseObservable.remove(this._onAnimationGroupPauseObserver);
  31. this._onAnimationGroupPauseObserver = null;
  32. }
  33. }
  34. connect(animationGroup: AnimationGroup) {
  35. this._onAnimationGroupPlayObserver = animationGroup.onAnimationGroupPlayObservable.add(() => {
  36. this.forceUpdate();
  37. });
  38. this._onAnimationGroupPauseObserver = animationGroup.onAnimationGroupPauseObservable.add(() => {
  39. this.forceUpdate();
  40. });
  41. this.updateCurrentFrame(animationGroup);
  42. }
  43. updateCurrentFrame(animationGroup: AnimationGroup) {
  44. var targetedAnimations = animationGroup.targetedAnimations;
  45. if (targetedAnimations.length > 0) {
  46. var runtimeAnimations = animationGroup.targetedAnimations[0].animation.runtimeAnimations;
  47. if (runtimeAnimations.length > 0) {
  48. this.setState({ currentFrame: runtimeAnimations[0].currentFrame });
  49. } else {
  50. this.setState({ currentFrame: 0 });
  51. }
  52. }
  53. }
  54. shouldComponentUpdate(nextProps: IAnimationGroupGridComponentProps): boolean {
  55. if (this.props.animationGroup !== nextProps.animationGroup) {
  56. this.disconnect(this.props.animationGroup);
  57. this.connect(nextProps.animationGroup);
  58. }
  59. return true;
  60. }
  61. componentWillMount() {
  62. this.connect(this.props.animationGroup);
  63. this._onBeforeRenderObserver = this.props.scene.onBeforeRenderObservable.add(() => {
  64. if (this.props.animationGroup.isPlaying) {
  65. this.updateCurrentFrame(this.props.animationGroup);
  66. }
  67. });
  68. }
  69. componentWillUnmount() {
  70. this.disconnect(this.props.animationGroup);
  71. if (this._onBeforeRenderObserver) {
  72. this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  73. this._onBeforeRenderObserver = null;
  74. }
  75. }
  76. playOrPause() {
  77. const animationGroup = this.props.animationGroup;
  78. if (animationGroup.isPlaying) {
  79. this.setState({ playButtonText: "Play" });
  80. animationGroup.pause();
  81. } else {
  82. this.setState({ playButtonText: "Pause" });
  83. this.props.scene.animationGroups.forEach(grp => grp.pause());
  84. animationGroup.play(true);
  85. }
  86. }
  87. onCurrentFrameChange(value: number) {
  88. const animationGroup = this.props.animationGroup;
  89. if (!animationGroup.isPlaying) {
  90. animationGroup.play(true);
  91. animationGroup.goToFrame(value);
  92. animationGroup.pause();
  93. } else {
  94. animationGroup.goToFrame(value);
  95. }
  96. this.setState({ currentFrame: value });
  97. }
  98. render() {
  99. const animationGroup = this.props.animationGroup;
  100. const playButtonText = animationGroup.isPlaying ? "Pause" : "Play"
  101. return (
  102. <div className="pane">
  103. <LineContainerComponent title="CONTROLS">
  104. <ButtonLineComponent label={playButtonText} onClick={() => this.playOrPause()} />
  105. <SliderLineComponent label="Speed ratio" minimum={0} maximum={10} step={0.1} target={animationGroup} propertyName="speedRatio" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  106. <SliderLineComponent ref="timeline" label="Current frame" minimum={animationGroup.from} maximum={animationGroup.to} step={(animationGroup.to - animationGroup.from) / 100.0} directValue={this.state.currentFrame} onInput={value => this.onCurrentFrameChange(value)} />
  107. </LineContainerComponent>
  108. <LineContainerComponent title="INFOS">
  109. <TextLineComponent label="Animation count" value={animationGroup.targetedAnimations.length.toString()} />
  110. <TextLineComponent label="From" value={animationGroup.from.toFixed(2)} />
  111. <TextLineComponent label="To" value={animationGroup.to.toFixed(2)} />
  112. </LineContainerComponent>
  113. </div>
  114. );
  115. }
  116. }