animationPropertyGridComponent.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import * as React from "react";
  2. import { Observable, Observer } from "babylonjs/Misc/observable";
  3. import { Scene } from "babylonjs/scene";
  4. import { PropertyChangedEvent } from "../../../propertyChangedEvent";
  5. import { ButtonLineComponent } from "../../lines/buttonLineComponent";
  6. import { LineContainerComponent } from "../../lineContainerComponent";
  7. import { SliderLineComponent } from "../../lines/sliderLineComponent";
  8. import { LockObject } from "./lockObject";
  9. import { GlobalState } from '../../../globalState';
  10. import { Animation } from 'babylonjs/Animations/animation';
  11. import { Animatable } from 'babylonjs/Animations/animatable';
  12. import { AnimationPropertiesOverride } from 'babylonjs/Animations/animationPropertiesOverride';
  13. import { AnimationRange } from 'babylonjs/Animations/animationRange';
  14. import { CheckBoxLineComponent } from '../../lines/checkBoxLineComponent';
  15. import { Nullable } from 'babylonjs/types';
  16. import { FloatLineComponent } from '../../lines/floatLineComponent';
  17. import { TextLineComponent } from '../../lines/textLineComponent';
  18. import { IAnimatable } from 'babylonjs/Animations/animatable.interface';
  19. interface IAnimationGridComponentProps {
  20. globalState: GlobalState;
  21. animatable: IAnimatable,
  22. scene: Scene,
  23. lockObject: LockObject,
  24. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  25. }
  26. export class AnimationGridComponent extends React.Component<IAnimationGridComponentProps, { currentFrame: number }> {
  27. private _animations: Nullable<Animation[]> = null;
  28. private _ranges: AnimationRange[];
  29. private _animationControl = {
  30. from: 0,
  31. to: 0,
  32. loop: false
  33. }
  34. private _runningAnimatable: Nullable<Animatable>;
  35. private _onBeforeRenderObserver: Nullable<Observer<Scene>>;
  36. private _isPlaying = false;
  37. private timelineRef: React.RefObject<SliderLineComponent>;
  38. constructor(props: IAnimationGridComponentProps) {
  39. super(props);
  40. this.state = { currentFrame: 0 };
  41. const animatableAsAny = this.props.animatable as any;
  42. this._ranges = animatableAsAny.getAnimationRanges ? animatableAsAny.getAnimationRanges() : [];
  43. if (animatableAsAny.getAnimatables) {
  44. const animatables = animatableAsAny.getAnimatables();
  45. this._animations = new Array<Animation>();
  46. animatables.forEach((animatable: IAnimatable) => {
  47. if (animatable.animations) {
  48. this._animations!.push(...animatable.animations);
  49. }
  50. });
  51. // Extract from and to
  52. if (this._animations && this._animations.length) {
  53. this._animations.forEach(animation => {
  54. let keys = animation.getKeys();
  55. if (keys && keys.length > 0) {
  56. if (keys[0].frame < this._animationControl.from) {
  57. this._animationControl.from = keys[0].frame;
  58. }
  59. const lastKeyIndex = keys.length - 1;
  60. if (keys[lastKeyIndex].frame > this._animationControl.to) {
  61. this._animationControl.to = keys[lastKeyIndex].frame;
  62. }
  63. }
  64. });
  65. }
  66. }
  67. this.timelineRef = React.createRef();
  68. }
  69. playOrPause() {
  70. const animatable = this.props.animatable;
  71. this._isPlaying = this.props.scene.getAllAnimatablesByTarget(animatable).length > 0;
  72. if (this._isPlaying) {
  73. this.props.scene.stopAnimation(this.props.animatable);
  74. this._runningAnimatable = null;
  75. } else {
  76. this._runningAnimatable = this.props.scene.beginAnimation(this.props.animatable, this._animationControl.from, this._animationControl.to, this._animationControl.loop);
  77. }
  78. this.forceUpdate();
  79. }
  80. componentDidMount() {
  81. this._onBeforeRenderObserver = this.props.scene.onBeforeRenderObservable.add(() => {
  82. if (!this._isPlaying || !this._runningAnimatable) {
  83. return;
  84. }
  85. this.setState({ currentFrame: this._runningAnimatable.masterFrame });
  86. });
  87. }
  88. componentWillUnmount() {
  89. if (this._onBeforeRenderObserver) {
  90. this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  91. this._onBeforeRenderObserver = null;
  92. }
  93. }
  94. onCurrentFrameChange(value: number) {
  95. if (!this._runningAnimatable) {
  96. return;
  97. }
  98. this._runningAnimatable.goToFrame(value);
  99. this.setState({ currentFrame: value });
  100. }
  101. render() {
  102. const animatable = this.props.animatable;
  103. const animatableAsAny = this.props.animatable as any;
  104. let animatablesForTarget = this.props.scene.getAllAnimatablesByTarget(animatable);
  105. this._isPlaying = animatablesForTarget.length > 0;
  106. if (this._isPlaying && !this._runningAnimatable) {
  107. this._runningAnimatable = animatablesForTarget[0];
  108. }
  109. if (this._runningAnimatable) {
  110. this._animationControl.from = this._runningAnimatable.fromFrame;
  111. this._animationControl.to = this._runningAnimatable.toFrame;
  112. this._animationControl.loop = this._runningAnimatable.loopAnimation;
  113. }
  114. return (
  115. <div>
  116. {
  117. (this._ranges.length > 0 || this._animations && this._animations.length > 0) &&
  118. <LineContainerComponent globalState={this.props.globalState} title="ANIMATION OVERRIDE">
  119. <CheckBoxLineComponent label="Enable override" onSelect={value => {
  120. if (value) {
  121. animatableAsAny.animationPropertiesOverride = new AnimationPropertiesOverride();
  122. animatableAsAny.animationPropertiesOverride.blendingSpeed = 0.05;
  123. } else {
  124. animatableAsAny.animationPropertiesOverride = null;
  125. }
  126. this.forceUpdate();
  127. }} isSelected={() => animatableAsAny.animationPropertiesOverride != null}
  128. onValueChanged={() => this.forceUpdate()}
  129. />
  130. {
  131. animatableAsAny.animationPropertiesOverride != null &&
  132. <div>
  133. <CheckBoxLineComponent label="Enable blending" target={animatableAsAny.animationPropertiesOverride} propertyName="enableBlending" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  134. <SliderLineComponent label="Blending speed" target={animatableAsAny.animationPropertiesOverride} propertyName="blendingSpeed" minimum={0} maximum={0.1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  135. </div>
  136. }
  137. </LineContainerComponent>
  138. }
  139. {
  140. this._ranges.length > 0 &&
  141. <LineContainerComponent globalState={this.props.globalState} title="ANIMATION RANGES">
  142. {
  143. this._ranges.map(range => {
  144. return (
  145. <ButtonLineComponent key={range.name} label={range.name}
  146. onClick={() => {
  147. this._runningAnimatable = null;
  148. this.props.scene.beginAnimation(animatable, range.from, range.to, true)
  149. }} />
  150. );
  151. })
  152. }
  153. </LineContainerComponent>
  154. }
  155. {
  156. this._animations && this._animations.length > 0 &&
  157. <LineContainerComponent globalState={this.props.globalState} title="ANIMATIONS">
  158. <TextLineComponent label="Count" value={this._animations.length.toString()} />
  159. <FloatLineComponent lockObject={this.props.lockObject} label="From" target={this._animationControl} propertyName="from" />
  160. <FloatLineComponent lockObject={this.props.lockObject} label="To" target={this._animationControl} propertyName="to" />
  161. <CheckBoxLineComponent label="Loop" onSelect={value => this._animationControl.loop = value} isSelected={() => this._animationControl.loop} />
  162. <ButtonLineComponent label={this._isPlaying ? "Stop" : "Play"} onClick={() => this.playOrPause()} />
  163. {
  164. this._isPlaying &&
  165. <SliderLineComponent ref={this.timelineRef} label="Current frame" minimum={this._animationControl.from} maximum={this._animationControl.to}
  166. step={(this._animationControl.to - this._animationControl.from) / 1000.0} directValue={this.state.currentFrame}
  167. onInput={value => this.onCurrentFrameChange(value)}
  168. />
  169. }
  170. </LineContainerComponent>
  171. }
  172. </div>
  173. );
  174. }
  175. }