animation.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { PluginUIComponent } from '../base';
  8. import { ParameterControls, ParamOnChange } from '../controls/parameters';
  9. import { Icon } from '../controls/icons';
  10. import { Button } from '../controls/common';
  11. import { Code, PlayArrow } from '@material-ui/icons';
  12. export class AnimationControlsWrapper extends PluginUIComponent<{ }> {
  13. render() {
  14. const anim = this.plugin.state.animation;
  15. if (anim.isEmpty) return null;
  16. return <div className='msp-controls-section'>
  17. <div className='msp-section-header'><Icon svg={Code} /> Animations</div>
  18. <AnimationControls />
  19. </div>;
  20. }
  21. }
  22. export class AnimationControls extends PluginUIComponent<{ onStart?: () => void }> {
  23. componentDidMount() {
  24. this.subscribe(this.plugin.state.animation.events.updated, () => this.forceUpdate());
  25. }
  26. updateParams: ParamOnChange = p => {
  27. this.plugin.state.animation.updateParams({ [p.name]: p.value });
  28. }
  29. updateCurrentParams: ParamOnChange = p => {
  30. this.plugin.state.animation.updateCurrentParams({ [p.name]: p.value });
  31. }
  32. startOrStop = () => {
  33. const anim = this.plugin.state.animation;
  34. if (anim.state.animationState === 'playing') anim.stop();
  35. else {
  36. if (this.props.onStart) this.props.onStart();
  37. anim.start();
  38. }
  39. }
  40. render() {
  41. const anim = this.plugin.state.animation;
  42. if (anim.isEmpty) return null;
  43. const isDisabled = anim.state.animationState === 'playing';
  44. return <>
  45. <ParameterControls params={anim.getParams()} values={anim.state.params} onChange={this.updateParams} isDisabled={isDisabled} />
  46. <ParameterControls params={anim.current.params} values={anim.current.paramValues} onChange={this.updateCurrentParams} isDisabled={isDisabled} />
  47. <div className='msp-flex-row'>
  48. <Button icon={anim.state.animationState !== 'playing' ? void 0 : PlayArrow} onClick={this.startOrStop}>
  49. {anim.state.animationState === 'playing' ? 'Stop' : 'Start'}
  50. </Button>
  51. </div>
  52. </>;
  53. }
  54. }