LayersViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using PixiEditor.Helpers;
  2. namespace PixiEditor.ViewModels.SubViewModels.Main
  3. {
  4. public class LayersViewModel : SubViewModel<ViewModelMain>
  5. {
  6. public RelayCommand SetActiveLayerCommand { get; set; }
  7. public RelayCommand NewLayerCommand { get; set; }
  8. public RelayCommand DeleteLayerCommand { get; set; }
  9. public RelayCommand RenameLayerCommand { get; set; }
  10. public RelayCommand MoveToBackCommand { get; set; }
  11. public RelayCommand MoveToFrontCommand { get; set; }
  12. public RelayCommand MergeWithAboveCommand { get; set; }
  13. public RelayCommand MergeWithBelowCommand { get; set; }
  14. public LayersViewModel(ViewModelMain owner)
  15. : base(owner)
  16. {
  17. SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
  18. NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
  19. DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
  20. MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
  21. MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
  22. RenameLayerCommand = new RelayCommand(RenameLayer);
  23. MergeWithAboveCommand = new RelayCommand(MergeWithAbove, CanMergeWithAbove);
  24. MergeWithBelowCommand = new RelayCommand(MergeWithBelow, CanMergeWithBelow);
  25. }
  26. public void NewLayer(object parameter)
  27. {
  28. Owner.BitmapManager.ActiveDocument.AddNewLayer($"New Layer {Owner.BitmapManager.ActiveDocument.Layers.Count}");
  29. }
  30. public bool CanCreateNewLayer(object parameter)
  31. {
  32. return Owner.BitmapManager.ActiveDocument != null && Owner.BitmapManager.ActiveDocument.Layers.Count > 0;
  33. }
  34. public void SetActiveLayer(object parameter)
  35. {
  36. Owner.BitmapManager.ActiveDocument.SetActiveLayer((int)parameter);
  37. }
  38. public void DeleteLayer(object parameter)
  39. {
  40. Owner.BitmapManager.ActiveDocument.RemoveLayer((int)parameter);
  41. }
  42. public bool CanDeleteLayer(object property)
  43. {
  44. return Owner.BitmapManager.ActiveDocument != null && Owner.BitmapManager.ActiveDocument.Layers.Count > 1;
  45. }
  46. public void RenameLayer(object parameter)
  47. {
  48. int? index = (int?)parameter;
  49. if (index == null)
  50. {
  51. index = Owner.BitmapManager.ActiveDocument.ActiveLayerIndex;
  52. }
  53. Owner.BitmapManager.ActiveDocument.Layers[index.Value].IsRenaming = true;
  54. }
  55. public bool CanRenameLayer(object parameter)
  56. {
  57. return Owner.BitmapManager.ActiveDocument != null;
  58. }
  59. public void MoveLayerToFront(object parameter)
  60. {
  61. int oldIndex = (int)parameter;
  62. Owner.BitmapManager.ActiveDocument.MoveLayerIndexBy(oldIndex, 1);
  63. }
  64. public void MoveLayerToBack(object parameter)
  65. {
  66. int oldIndex = (int)parameter;
  67. Owner.BitmapManager.ActiveDocument.MoveLayerIndexBy(oldIndex, -1);
  68. }
  69. public bool CanMoveToFront(object property)
  70. {
  71. return Owner.DocumentIsNotNull(null) && Owner.BitmapManager.ActiveDocument.Layers.Count - 1 > (int)property;
  72. }
  73. public bool CanMoveToBack(object property)
  74. {
  75. return (int)property > 0;
  76. }
  77. public void MergeWithAbove(object parameter)
  78. {
  79. int index = (int)parameter;
  80. Owner.BitmapManager.ActiveDocument.MergeLayers(index, index + 1, false);
  81. }
  82. public void MergeWithBelow(object parameter)
  83. {
  84. int index = (int)parameter;
  85. Owner.BitmapManager.ActiveDocument.MergeLayers(index, index - 1, true);
  86. }
  87. public bool CanMergeWithAbove(object propery)
  88. {
  89. int index = (int)propery;
  90. return Owner.DocumentIsNotNull(null) && index != Owner.BitmapManager.ActiveDocument.Layers.Count - 1;
  91. }
  92. public bool CanMergeWithBelow(object propery)
  93. {
  94. int index = (int)propery;
  95. return Owner.DocumentIsNotNull(null) && index != 0;
  96. }
  97. }
  98. }