animationPropertyGridComponent.tsx 9.2 KB

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