animationPropertyGridComponent.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. import { AnimationCurveEditorComponent } from '../animations/animationCurveEditorComponent';
  20. import { PopupComponent } from '../animations/popupComponent';
  21. interface IAnimationGridComponentProps {
  22. globalState: GlobalState;
  23. animatable: IAnimatable,
  24. scene: Scene,
  25. lockObject: LockObject,
  26. onPropertyChangedObservable?: Observable<PropertyChangedEvent>
  27. }
  28. export class AnimationGridComponent extends React.Component<IAnimationGridComponentProps, { currentFrame: number }> {
  29. private _animations: Nullable<Animation[]> = null;
  30. private _ranges: AnimationRange[];
  31. private _mainAnimatable: Nullable<Animatable>;
  32. private _onBeforeRenderObserver: Nullable<Observer<Scene>>;
  33. private _isPlaying = false;
  34. private timelineRef: React.RefObject<SliderLineComponent>;
  35. private _isCurveEditorOpen = false;
  36. private _animationControl = {
  37. from: 0,
  38. to: 0,
  39. loop: false
  40. }
  41. constructor(props: IAnimationGridComponentProps) {
  42. super(props);
  43. this.state = { currentFrame: 0 };
  44. const animatableAsAny = this.props.animatable as any;
  45. this._ranges = animatableAsAny.getAnimationRanges ? animatableAsAny.getAnimationRanges() : [];
  46. if (animatableAsAny.getAnimatables) {
  47. const animatables = animatableAsAny.getAnimatables();
  48. this._animations = new Array<Animation>();
  49. animatables.forEach((animatable: IAnimatable) => {
  50. if (animatable.animations) {
  51. this._animations!.push(...animatable.animations);
  52. }
  53. });
  54. if (animatableAsAny.animations) {
  55. this._animations!.push(...animatableAsAny.animations);
  56. }
  57. // Extract from and to
  58. if (this._animations && this._animations.length) {
  59. this._animations.forEach(animation => {
  60. let keys = animation.getKeys();
  61. if (keys && keys.length > 0) {
  62. if (keys[0].frame < this._animationControl.from) {
  63. this._animationControl.from = keys[0].frame;
  64. }
  65. const lastKeyIndex = keys.length - 1;
  66. if (keys[lastKeyIndex].frame > this._animationControl.to) {
  67. this._animationControl.to = keys[lastKeyIndex].frame;
  68. }
  69. }
  70. });
  71. }
  72. }
  73. this.timelineRef = React.createRef();
  74. }
  75. playOrPause() {
  76. const animatable = this.props.animatable;
  77. this._isPlaying = this.props.scene.getAllAnimatablesByTarget(animatable).length > 0;
  78. if (this._isPlaying) {
  79. this.props.scene.stopAnimation(this.props.animatable);
  80. this._mainAnimatable = null;
  81. } else {
  82. this._mainAnimatable = this.props.scene.beginAnimation(this.props.animatable, this._animationControl.from, this._animationControl.to, this._animationControl.loop);
  83. }
  84. this.forceUpdate();
  85. }
  86. componentDidMount() {
  87. this._onBeforeRenderObserver = this.props.scene.onBeforeRenderObservable.add(() => {
  88. if (!this._isPlaying || !this._mainAnimatable) {
  89. return;
  90. }
  91. this.setState({ currentFrame: this._mainAnimatable.masterFrame });
  92. });
  93. }
  94. componentWillUnmount() {
  95. if (this._onBeforeRenderObserver) {
  96. this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  97. this._onBeforeRenderObserver = null;
  98. }
  99. }
  100. onCurrentFrameChange(value: number) {
  101. if (!this._mainAnimatable) {
  102. return;
  103. }
  104. this._mainAnimatable.goToFrame(value);
  105. this.setState({ currentFrame: value });
  106. }
  107. onChangeFromOrTo() {
  108. this.playOrPause();
  109. if (this._isPlaying) {
  110. this.playOrPause();
  111. }
  112. }
  113. onOpenAnimationCurveEditor() {
  114. this._isCurveEditorOpen = true;
  115. }
  116. onCloseAnimationCurveEditor(window: Window | null) {
  117. this._isCurveEditorOpen = false;
  118. if (window === null) {
  119. console.log("Window already closed");
  120. } else {
  121. window.close();
  122. }
  123. }
  124. render() {
  125. const animatable = this.props.animatable;
  126. const animatableAsAny = this.props.animatable as any;
  127. let animatablesForTarget = this.props.scene.getAllAnimatablesByTarget(animatable);
  128. this._isPlaying = animatablesForTarget.length > 0;
  129. if (this._isPlaying && !this._mainAnimatable) {
  130. this._mainAnimatable = animatablesForTarget[0];
  131. if (this._mainAnimatable) {
  132. this._animationControl.from = this._mainAnimatable.fromFrame;
  133. this._animationControl.to = this._mainAnimatable.toFrame;
  134. this._animationControl.loop = this._mainAnimatable.loopAnimation;
  135. }
  136. }
  137. let animations = animatable.animations;
  138. return (
  139. <div>
  140. {
  141. this._ranges.length > 0 &&
  142. <LineContainerComponent globalState={this.props.globalState} title="ANIMATION RANGES">
  143. {
  144. this._ranges.map((range, i) => {
  145. return (
  146. <ButtonLineComponent key={range.name + i} label={range.name}
  147. onClick={() => {
  148. this._mainAnimatable = null;
  149. this.props.scene.beginAnimation(animatable, range.from, range.to, true)
  150. }} />
  151. );
  152. })
  153. }
  154. </LineContainerComponent>
  155. }
  156. {
  157. animations && animations.length > 0 &&
  158. <>
  159. <LineContainerComponent globalState={this.props.globalState} title="ANIMATIONS">
  160. <TextLineComponent label="Count" value={animations.length.toString()} />
  161. <ButtonLineComponent label="Edit" onClick={() => this.onOpenAnimationCurveEditor()} />
  162. {
  163. animations.map((anim, i) => {
  164. return (
  165. <TextLineComponent key={anim.targetProperty + i} label={"#" + i + " >"} value={anim.targetProperty} />
  166. )
  167. })
  168. }
  169. {
  170. this._isCurveEditorOpen && <PopupComponent
  171. id="curve-editor"
  172. title="Curve Animation Editor"
  173. size={{ width: 800, height: 600 }}
  174. onOpen={(window: Window) => { window.console.log("Window opened!!") }}
  175. onClose={(window: Window) => this.onCloseAnimationCurveEditor(window)}>
  176. <AnimationCurveEditorComponent title="Animations Curve Editor" close={(event) => this.onCloseAnimationCurveEditor(event.view)}/>
  177. </PopupComponent>
  178. }
  179. </LineContainerComponent>
  180. <LineContainerComponent globalState={this.props.globalState} title="ANIMATION GENERAL CONTROL">
  181. <FloatLineComponent lockObject={this.props.lockObject} isInteger={true} label="From" target={this._animationControl} propertyName="from" onChange={() => this.onChangeFromOrTo()} />
  182. <FloatLineComponent lockObject={this.props.lockObject} isInteger={true} label="To" target={this._animationControl} propertyName="to" onChange={() => this.onChangeFromOrTo()} />
  183. <CheckBoxLineComponent label="Loop" onSelect={value => this._animationControl.loop = value} isSelected={() => this._animationControl.loop} />
  184. {
  185. this._isPlaying &&
  186. <SliderLineComponent ref={this.timelineRef} label="Current frame" minimum={this._animationControl.from} maximum={this._animationControl.to}
  187. step={(this._animationControl.to - this._animationControl.from) / 1000.0} directValue={this.state.currentFrame}
  188. onInput={value => this.onCurrentFrameChange(value)}
  189. />
  190. }
  191. <ButtonLineComponent label={this._isPlaying ? "Stop" : "Play"} onClick={() => this.playOrPause()} />
  192. {
  193. (this._ranges.length > 0 || this._animations && this._animations.length > 0) &&
  194. <>
  195. <CheckBoxLineComponent label="Enable override" onSelect={value => {
  196. if (value) {
  197. animatableAsAny.animationPropertiesOverride = new AnimationPropertiesOverride();
  198. animatableAsAny.animationPropertiesOverride.blendingSpeed = 0.05;
  199. } else {
  200. animatableAsAny.animationPropertiesOverride = null;
  201. }
  202. this.forceUpdate();
  203. }} isSelected={() => animatableAsAny.animationPropertiesOverride != null}
  204. onValueChanged={() => this.forceUpdate()}
  205. />
  206. {
  207. animatableAsAny.animationPropertiesOverride != null &&
  208. <div>
  209. <CheckBoxLineComponent label="Enable blending" target={animatableAsAny.animationPropertiesOverride} propertyName="enableBlending" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  210. <SliderLineComponent label="Blending speed" target={animatableAsAny.animationPropertiesOverride} propertyName="blendingSpeed" minimum={0} maximum={0.1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
  211. </div>
  212. }
  213. </>
  214. }
  215. </LineContainerComponent>
  216. </>
  217. }
  218. </div>
  219. );
  220. }
  221. }