controls.tsx 6.2 KB

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