animation.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 { Button } from '../controls/common';
  10. import { PlayArrowSvg } from '../controls/icons';
  11. export class AnimationControls extends PluginUIComponent<{ onStart?: () => void }> {
  12. componentDidMount() {
  13. this.subscribe(this.plugin.managers.animation.events.updated, () => this.forceUpdate());
  14. }
  15. updateParams: ParamOnChange = p => {
  16. this.plugin.managers.animation.updateParams({ [p.name]: p.value });
  17. }
  18. updateCurrentParams: ParamOnChange = p => {
  19. this.plugin.managers.animation.updateCurrentParams({ [p.name]: p.value });
  20. }
  21. startOrStop = () => {
  22. const anim = this.plugin.managers.animation;
  23. if (anim.state.animationState === 'playing') anim.stop();
  24. else {
  25. if (this.props.onStart) this.props.onStart();
  26. anim.start();
  27. }
  28. }
  29. render() {
  30. const anim = this.plugin.managers.animation;
  31. if (anim.isEmpty) return null;
  32. const isDisabled = anim.state.animationState === 'playing';
  33. const canApply = anim.current.anim.canApply?.(this.plugin);
  34. return <>
  35. <ParameterControls params={anim.getParams()} values={anim.state.params} onChange={this.updateParams} isDisabled={isDisabled} />
  36. <ParameterControls params={anim.current.params} values={anim.current.paramValues} onChange={this.updateCurrentParams} isDisabled={isDisabled} />
  37. <div className='msp-flex-row'>
  38. <Button icon={anim.state.animationState !== 'playing' ? void 0 : PlayArrowSvg} onClick={this.startOrStop} disabled={canApply !== void 0 && canApply.canApply === false}>
  39. {anim.state.animationState === 'playing' ? 'Stop' : canApply === void 0 || canApply.canApply ? 'Start' : canApply.reason || 'Start'}
  40. </Button>
  41. </div>
  42. </>;
  43. }
  44. }