AnimationsViewModel.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using PixiEditor.AnimationRenderer.Core;
  2. using PixiEditor.AvaloniaUI.Models.Commands.Attributes.Commands;
  3. using PixiEditor.AvaloniaUI.Models.IO;
  4. using PixiEditor.AvaloniaUI.ViewModels.Document;
  5. namespace PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
  6. [Command.Group("PixiEditor.Animations", "ANIMATIONS")]
  7. internal class AnimationsViewModel : SubViewModel<ViewModelMain>
  8. {
  9. private IAnimationRenderer animationRenderer;
  10. public AnimationsViewModel(ViewModelMain owner, IAnimationRenderer renderer) : base(owner)
  11. {
  12. animationRenderer = renderer;
  13. }
  14. [Command.Basic("PixiEditor.Animations.RenderAnimation", "Render Animation (MP4)", "Renders the animation as an MP4 file")]
  15. public async Task RenderAnimation()
  16. {
  17. var document = Owner.DocumentManagerSubViewModel.ActiveDocument;
  18. if (document is null)
  19. return;
  20. document.RenderFrames(Paths.TempRenderingPath);
  21. await animationRenderer.RenderAsync(Paths.TempRenderingPath);
  22. }
  23. [Command.Basic("PixiEditor.Animation.CreateRasterKeyFrame", "Create Raster Key Frame", "Create a raster key frame", Parameter = false)]
  24. [Command.Basic("PixiEditor.Animation.DuplicateRasterKeyFrame", "Duplicate Raster Key Frame", "Duplicate a raster key frame", Parameter = true)]
  25. public void CreateRasterKeyFrame(bool duplicate)
  26. {
  27. var activeDocument = Owner.DocumentManagerSubViewModel.ActiveDocument;
  28. if (activeDocument == null || activeDocument.SelectedStructureMember is null)
  29. {
  30. return;
  31. }
  32. int newFrame = 0;
  33. if (activeDocument.AnimationDataViewModel.TryFindKeyFrame(activeDocument.SelectedStructureMember.GuidValue, out KeyFrameGroupViewModel group))
  34. {
  35. newFrame = group.StartFrame + group.Duration;
  36. }
  37. activeDocument.AnimationDataViewModel.CreateRasterKeyFrame(
  38. activeDocument.SelectedStructureMember.GuidValue,
  39. newFrame,
  40. duplicate);
  41. activeDocument.Operations.SetActiveFrame(newFrame);
  42. }
  43. [Command.Internal("PixiEditor.Document.StartChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  44. public void StartChangeActiveFrame(int newActiveFrame)
  45. {
  46. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  47. return;
  48. Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.StartChangeActiveFrame();
  49. Owner.DocumentManagerSubViewModel.ActiveDocument.Tools.UseActiveFrame(newActiveFrame);
  50. }
  51. [Command.Internal("PixiEditor.Document.ChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  52. public void ChangeActiveFrame(double newActiveFrame)
  53. {
  54. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  55. return;
  56. int intNewActiveFrame = (int)newActiveFrame;
  57. Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.ChangeActiveFrame(intNewActiveFrame);
  58. }
  59. [Command.Internal("PixiEditor.Document.EndChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  60. public void EndChangeActiveFrame()
  61. {
  62. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  63. return;
  64. Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.EndChangeActiveFrame();
  65. }
  66. [Command.Internal("PixiEditor.Animation.ActiveFrameSet")]
  67. public void ActiveFrameSet(double value)
  68. {
  69. var document = Owner.DocumentManagerSubViewModel.ActiveDocument;
  70. if (document is null)
  71. return;
  72. document.Operations.SetActiveFrame((int)value);
  73. }
  74. }