controls.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as React from "react";
  2. import { IAnimationKey } from "babylonjs/Animations/animationKey";
  3. import { IconButtonLineComponent } from "../../../lines/iconButtonLineComponent";
  4. interface IControlsProps {
  5. // Keyframes to choose start or end of animation playback
  6. keyframes: IAnimationKey[] | null;
  7. // The currently selected animation keyframe
  8. selected: IAnimationKey | null;
  9. // The current frame number
  10. currentFrame: number;
  11. // Event to change the current frame
  12. onCurrentFrameChange: (frame: number) => void;
  13. // Event to communicate canvas repposition
  14. repositionCanvas: (keyframe: IAnimationKey) => void;
  15. // Event to play, pause or play backwards the animation
  16. playPause: (direction: number) => void;
  17. // If the animation is playing
  18. isPlaying: boolean;
  19. // The reference to the scrollable dom object to set its position
  20. scrollable: React.RefObject<HTMLDivElement>;
  21. }
  22. /**
  23. * The playback controls for the animation editor
  24. */
  25. export class Controls extends React.Component<IControlsProps, { selected: IAnimationKey; playingType: string }> {
  26. readonly _sizeOfKeyframe: number = 5;
  27. constructor(props: IControlsProps) {
  28. super(props);
  29. if (this.props.selected !== null) {
  30. this.state = { selected: this.props.selected, playingType: "" };
  31. }
  32. }
  33. playBackwards = () => {
  34. this.setState({ playingType: "reverse" });
  35. this.props.playPause(-1);
  36. };
  37. play = () => {
  38. this.setState({ playingType: "forward" });
  39. this.props.playPause(1);
  40. };
  41. pause = () => {
  42. if (this.props.isPlaying) {
  43. this.setState({ playingType: "" });
  44. this.props.playPause(0);
  45. }
  46. };
  47. moveToAnimationStart = () => {
  48. const startKeyframe = this.props.keyframes && this.props.keyframes[0];
  49. if (startKeyframe !== null) {
  50. if (typeof startKeyframe.frame === "number") {
  51. this.props.onCurrentFrameChange(startKeyframe.frame);
  52. }
  53. }
  54. };
  55. moveToAnimationEnd = () => {
  56. const endKeyframe = this.props.keyframes && this.props.keyframes[this.props.keyframes.length - 1];
  57. if (endKeyframe !== null) {
  58. if (typeof endKeyframe.frame === "number") {
  59. this.props.onCurrentFrameChange(endKeyframe.frame);
  60. }
  61. }
  62. };
  63. nextKeyframe = () => {
  64. if (this.props.keyframes !== null) {
  65. let first = this.props.keyframes.find((kf) => kf.frame > this.props.currentFrame);
  66. if (first) {
  67. this.props.onCurrentFrameChange(first.frame);
  68. this.setState({ selected: first });
  69. (this.props.scrollable.current as HTMLDivElement).scrollLeft = first.frame * this._sizeOfKeyframe;
  70. }
  71. }
  72. };
  73. previousKeyframe = () => {
  74. if (this.props.keyframes !== null) {
  75. let keyframes = [...this.props.keyframes];
  76. let first = keyframes.reverse().find((kf) => kf.frame < this.props.currentFrame);
  77. if (first) {
  78. this.props.onCurrentFrameChange(first.frame);
  79. this.setState({ selected: first });
  80. (this.props.scrollable.current as HTMLDivElement).scrollLeft = -(first.frame * this._sizeOfKeyframe);
  81. }
  82. }
  83. };
  84. render() {
  85. return (
  86. <div className="controls">
  87. <IconButtonLineComponent tooltip="Animation Start" icon="animation-start" onClick={this.moveToAnimationStart}></IconButtonLineComponent>
  88. <IconButtonLineComponent tooltip="Previous Keyframe" icon="animation-lastkey" onClick={this.previousKeyframe}></IconButtonLineComponent>
  89. {this.props.isPlaying ? (
  90. <div className="stop-container">
  91. {this.state.playingType === "reverse" ? (
  92. <>
  93. <IconButtonLineComponent tooltip="Pause" icon="animation-stop" onClick={this.pause}></IconButtonLineComponent>
  94. <IconButtonLineComponent tooltip="Play Forward" icon="animation-playfwd" onClick={this.play}></IconButtonLineComponent>
  95. </>
  96. ) : (
  97. <>
  98. <IconButtonLineComponent tooltip="Play Reverse" icon="animation-playrev" onClick={this.playBackwards}></IconButtonLineComponent>
  99. <IconButtonLineComponent tooltip="Pause" icon="animation-stop" onClick={this.pause}></IconButtonLineComponent>
  100. </>
  101. )}
  102. </div>
  103. ) : (
  104. <div className="stop-container">
  105. <IconButtonLineComponent tooltip="Play Reverse" icon="animation-playrev" onClick={this.playBackwards}></IconButtonLineComponent>
  106. <IconButtonLineComponent tooltip="Play Forward" icon="animation-playfwd" onClick={this.play}></IconButtonLineComponent>
  107. </div>
  108. )}
  109. <IconButtonLineComponent tooltip="Next Keyframe" icon="animation-nextkey" onClick={this.nextKeyframe}></IconButtonLineComponent>
  110. <IconButtonLineComponent tooltip="Animation End" icon="animation-end" onClick={this.moveToAnimationEnd}></IconButtonLineComponent>
  111. </div>
  112. );
  113. }
  114. }