AnimationsViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using ChunkyImageLib;
  2. using PixiEditor.AnimationRenderer.Core;
  3. using PixiEditor.AvaloniaUI.Models.Commands.Attributes.Commands;
  4. using PixiEditor.AvaloniaUI.Models.IO;
  5. using PixiEditor.AvaloniaUI.ViewModels.Document;
  6. using PixiEditor.DrawingApi.Core.Surface;
  7. using PixiEditor.DrawingApi.Core.Surface.ImageData;
  8. using PixiEditor.Numerics;
  9. namespace PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
  10. [Command.Group("PixiEditor.Animations", "ANIMATIONS")]
  11. internal class AnimationsViewModel : SubViewModel<ViewModelMain>
  12. {
  13. public AnimationsViewModel(ViewModelMain owner) : base(owner)
  14. {
  15. }
  16. [Command.Basic("PixiEditor.Animation.CreateRasterKeyFrame", "Create Raster Key Frame", "Create a raster key frame", Parameter = false)]
  17. [Command.Basic("PixiEditor.Animation.DuplicateRasterKeyFrame", "Duplicate Raster Key Frame", "Duplicate a raster key frame", Parameter = true)]
  18. public void CreateRasterKeyFrame(bool duplicate)
  19. {
  20. var activeDocument = Owner.DocumentManagerSubViewModel.ActiveDocument;
  21. if (activeDocument?.SelectedStructureMember is null)
  22. {
  23. return;
  24. }
  25. int newFrame = GetActiveFrame(activeDocument, activeDocument.SelectedStructureMember.Id);
  26. Guid toCloneFrom = duplicate ? activeDocument.SelectedStructureMember.Id : Guid.Empty;
  27. int frameToCopyFrom = duplicate ? activeDocument.AnimationDataViewModel.ActiveFrameBindable : -1;
  28. activeDocument.AnimationDataViewModel.CreateRasterKeyFrame(
  29. activeDocument.SelectedStructureMember.Id,
  30. newFrame,
  31. toCloneFrom,
  32. frameToCopyFrom);
  33. activeDocument.Operations.SetActiveFrame(newFrame);
  34. }
  35. [Command.Basic("PixiEditor.Animation.DeleteKeyFrames", "Delete key frames", "Delete key frames")]
  36. public void DeleteKeyFrames(IList<KeyFrameViewModel> keyFrames)
  37. {
  38. var activeDocument = Owner.DocumentManagerSubViewModel.ActiveDocument;
  39. if (activeDocument is null)
  40. return;
  41. List<Guid> keyFrameIds = keyFrames.Select(x => x.Id).ToList();
  42. for(int i = 0; i < keyFrameIds.Count; i++)
  43. {
  44. if(!activeDocument.AnimationDataViewModel.TryFindKeyFrame<KeyFrameViewModel>(keyFrameIds[i], out _))
  45. {
  46. keyFrameIds.RemoveAt(i);
  47. i--;
  48. }
  49. }
  50. activeDocument.AnimationDataViewModel.DeleteKeyFrames(keyFrameIds);
  51. }
  52. [Command.Internal("PixiEditor.Animation.ChangeKeyFramesStartPos")]
  53. public void ChangeKeyFramesStartPos((Guid[] ids, int delta, bool end) info)
  54. {
  55. var activeDocument = Owner.DocumentManagerSubViewModel.ActiveDocument;
  56. if (activeDocument is null)
  57. return;
  58. if (!info.end)
  59. {
  60. activeDocument.AnimationDataViewModel.ChangeKeyFramesStartPos(info.ids, info.delta);
  61. }
  62. else
  63. {
  64. activeDocument.AnimationDataViewModel.EndKeyFramesStartPos();
  65. }
  66. }
  67. private static int GetActiveFrame(DocumentViewModel activeDocument, Guid targetLayer)
  68. {
  69. int active = activeDocument.AnimationDataViewModel.ActiveFrameBindable;
  70. if (activeDocument.AnimationDataViewModel.TryFindKeyFrame<KeyFrameGroupViewModel>(targetLayer, out KeyFrameGroupViewModel groupViewModel))
  71. {
  72. if(active == groupViewModel.StartFrameBindable + groupViewModel.DurationBindable - 1)
  73. {
  74. return groupViewModel.StartFrameBindable + groupViewModel.DurationBindable;
  75. }
  76. }
  77. return active;
  78. }
  79. [Command.Internal("PixiEditor.Document.StartChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  80. public void StartChangeActiveFrame(int newActiveFrame)
  81. {
  82. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  83. return;
  84. // TODO: same as below
  85. //Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.StartChangeActiveFrame();
  86. }
  87. [Command.Internal("PixiEditor.Document.ChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  88. public void ChangeActiveFrame(double newActiveFrame)
  89. {
  90. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  91. return;
  92. int intNewActiveFrame = (int)newActiveFrame;
  93. // TODO: Check if this should be implemented
  94. //Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.ChangeActiveFrame(intNewActiveFrame);
  95. }
  96. [Command.Internal("PixiEditor.Document.EndChangeActiveFrame", CanExecute = "PixiEditor.HasDocument")]
  97. public void EndChangeActiveFrame()
  98. {
  99. if (Owner.DocumentManagerSubViewModel.ActiveDocument is null)
  100. return;
  101. //Owner.DocumentManagerSubViewModel.ActiveDocument.EventInlet.EndChangeActiveFrame();
  102. }
  103. [Command.Internal("PixiEditor.Animation.ActiveFrameSet")]
  104. public void ActiveFrameSet(double value)
  105. {
  106. var document = Owner.DocumentManagerSubViewModel.ActiveDocument;
  107. if (document is null)
  108. return;
  109. document.Operations.SetActiveFrame((int)value);
  110. }
  111. }