LayersViewModel.cs 4.9 KB

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