CelViewModel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using ChunkyImageLib;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using PixiEditor.ChangeableDocument.Actions.Generated;
  4. using Drawie.Backend.Core;
  5. using PixiEditor.Models.DocumentModels;
  6. using PixiEditor.Models.Handlers;
  7. using PixiEditor.Models.Rendering;
  8. namespace PixiEditor.ViewModels.Document;
  9. internal abstract class CelViewModel : ObservableObject, ICelHandler
  10. {
  11. private PreviewPainter? previewPainter;
  12. private int startFrameBindable;
  13. private int durationBindable;
  14. private bool isVisibleBindable = true;
  15. private bool isSelected;
  16. private bool isCollapsed;
  17. public bool IsCollapsed
  18. {
  19. get => isCollapsed;
  20. set => SetProperty(ref isCollapsed, value);
  21. }
  22. public DocumentViewModel Document { get; }
  23. protected DocumentInternalParts Internals { get; }
  24. IDocument ICelHandler.Document => Document;
  25. public PreviewPainter? PreviewPainter
  26. {
  27. get => previewPainter;
  28. set => SetProperty(ref previewPainter, value);
  29. }
  30. public virtual int StartFrameBindable
  31. {
  32. get => startFrameBindable;
  33. set
  34. {
  35. if (value < 0)
  36. {
  37. value = 0;
  38. }
  39. if (!Document.BlockingUpdateableChangeActive)
  40. {
  41. Internals.ActionAccumulator.AddFinishedActions(
  42. new KeyFrameLength_Action(Id, value, DurationBindable),
  43. new EndKeyFrameLength_Action());
  44. }
  45. }
  46. }
  47. public virtual int DurationBindable
  48. {
  49. get => durationBindable;
  50. set
  51. {
  52. if (value < 1)
  53. {
  54. value = 1;
  55. }
  56. if (!Document.BlockingUpdateableChangeActive)
  57. {
  58. Internals.ActionAccumulator.AddFinishedActions(
  59. new KeyFrameLength_Action(Id, StartFrameBindable, value),
  60. new EndKeyFrameLength_Action());
  61. }
  62. }
  63. }
  64. public virtual bool IsVisible
  65. {
  66. get => isVisibleBindable;
  67. set
  68. {
  69. if (!Document.BlockingUpdateableChangeActive)
  70. {
  71. Internals.ActionAccumulator.AddFinishedActions(new KeyFrameVisibility_Action(Id, value));
  72. }
  73. }
  74. }
  75. public Guid LayerGuid { get; }
  76. public Guid Id { get; }
  77. public bool IsSelected
  78. {
  79. get => isSelected;
  80. set
  81. {
  82. if (StartFrameBindable == 0) return;
  83. SetProperty(ref isSelected, value);
  84. }
  85. }
  86. protected CelViewModel(int startFrame, int duration, Guid layerGuid, Guid id,
  87. DocumentViewModel document, DocumentInternalParts internalParts)
  88. {
  89. startFrameBindable = startFrame;
  90. durationBindable = duration;
  91. LayerGuid = layerGuid;
  92. Id = id;
  93. Document = document;
  94. Internals = internalParts;
  95. }
  96. public void SetStartFrame(int newStartFrame)
  97. {
  98. startFrameBindable = newStartFrame;
  99. OnPropertyChanged(nameof(StartFrameBindable));
  100. }
  101. public void SetDuration(int newDuration)
  102. {
  103. durationBindable = newDuration;
  104. OnPropertyChanged(nameof(DurationBindable));
  105. }
  106. public void ChangeFrameLength(int newStartFrame, int newDuration)
  107. {
  108. newStartFrame = Math.Max(0, newStartFrame);
  109. newDuration = Math.Max(1, newDuration);
  110. Internals.ActionAccumulator.AddActions(
  111. new KeyFrameLength_Action(Id, newStartFrame, newDuration));
  112. }
  113. public void EndChangeFrameLength()
  114. {
  115. Internals.ActionAccumulator.AddFinishedActions(new EndKeyFrameLength_Action());
  116. }
  117. public virtual void SetVisibility(bool isVisible)
  118. {
  119. isVisibleBindable = isVisible;
  120. OnPropertyChanged(nameof(IsVisible));
  121. }
  122. public bool IsWithinRange(int frame)
  123. {
  124. return frame >= StartFrameBindable && frame < StartFrameBindable + DurationBindable;
  125. }
  126. public void Dispose()
  127. {
  128. PreviewPainter?.Dispose();
  129. PreviewPainter = null;
  130. }
  131. }