animationGroupPropertyGridComponent.tsx 5.7 KB

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