LayerGroupControl.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media.Imaging;
  7. using PixiEditor.Models.Controllers;
  8. using PixiEditor.Models.ImageManipulation;
  9. using PixiEditor.Models.Layers;
  10. using PixiEditor.Models.Undo;
  11. using PixiEditor.ViewModels.SubViewModels.Main;
  12. namespace PixiEditor.Views.UserControls
  13. {
  14. /// <summary>
  15. /// Interaction logic for LayerFolder.xaml.
  16. /// </summary>
  17. public partial class LayerGroupControl : UserControl
  18. {
  19. public Guid GroupGuid
  20. {
  21. get { return (Guid)GetValue(GroupGuidProperty); }
  22. set { SetValue(GroupGuidProperty, value); }
  23. }
  24. private const string LayerGroupControlDataName = "PixiEditor.Views.UserControls.LayerGroupControl";
  25. private const string LayerContainerDataName = "PixiEditor.Views.UserControls.LayerStructureItemContainer";
  26. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  27. public static readonly DependencyProperty GroupGuidProperty =
  28. DependencyProperty.Register("GroupGuid", typeof(Guid), typeof(LayerGroupControl), new PropertyMetadata(Guid.NewGuid()));
  29. public LayersViewModel LayersViewModel
  30. {
  31. get { return (LayersViewModel)GetValue(LayersViewModelProperty); }
  32. set { SetValue(LayersViewModelProperty, value); }
  33. }
  34. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  35. public static readonly DependencyProperty LayersViewModelProperty =
  36. DependencyProperty.Register("LayersViewModel", typeof(LayersViewModel), typeof(LayerGroupControl), new PropertyMetadata(default(LayersViewModel), LayersViewModelCallback));
  37. public bool IsVisibleUndoTriggerable
  38. {
  39. get { return (bool)GetValue(IsVisibleUndoTriggerableProperty); }
  40. set { SetValue(IsVisibleUndoTriggerableProperty, value); }
  41. }
  42. // Using a DependencyProperty as the backing store for IsVisibleUndoTriggerable. This enables animation, styling, binding, etc...
  43. public static readonly DependencyProperty IsVisibleUndoTriggerableProperty =
  44. DependencyProperty.Register("IsVisibleUndoTriggerable", typeof(bool), typeof(LayerGroupControl), new PropertyMetadata(true, IsVisibleChangedCallback));
  45. private static void IsVisibleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  46. {
  47. LayerGroupControl control = (LayerGroupControl)d;
  48. var doc = control.LayersViewModel.Owner.BitmapManager.ActiveDocument;
  49. var layers = doc.LayerStructure.GetGroupLayers(control.GroupData);
  50. foreach (var layer in layers)
  51. {
  52. layer.IsVisible = (bool)e.NewValue;
  53. }
  54. doc.UndoManager.AddUndoChange(
  55. new Change(
  56. nameof(IsVisibleUndoTriggerable),
  57. e.OldValue,
  58. e.NewValue,
  59. $"Change {control.GroupName} visibility",
  60. control), true);
  61. }
  62. private static void LayersViewModelCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  63. {
  64. LayerGroupControl control = (LayerGroupControl)d;
  65. if(e.OldValue is LayersViewModel oldVm && oldVm != e.NewValue)
  66. {
  67. oldVm.Owner.BitmapManager.MouseController.StoppedRecordingChanges -= control.MouseController_StoppedRecordingChanges;
  68. }
  69. if(e.NewValue is LayersViewModel vm)
  70. {
  71. vm.Owner.BitmapManager.MouseController.StoppedRecordingChanges += control.MouseController_StoppedRecordingChanges;
  72. }
  73. }
  74. public string GroupName
  75. {
  76. get { return (string)GetValue(GroupNameProperty); }
  77. set { SetValue(GroupNameProperty, value); }
  78. }
  79. // Using a DependencyProperty as the backing store for FolderName. This enables animation, styling, binding, etc...
  80. public static readonly DependencyProperty GroupNameProperty =
  81. DependencyProperty.Register("GroupName", typeof(string), typeof(LayerGroupControl), new PropertyMetadata(default(string)));
  82. public GuidStructureItem GroupData
  83. {
  84. get { return (GuidStructureItem)GetValue(GroupDataProperty); }
  85. set { SetValue(GroupDataProperty, value); }
  86. }
  87. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  88. public static readonly DependencyProperty GroupDataProperty =
  89. DependencyProperty.Register("GroupData", typeof(GuidStructureItem), typeof(LayerGroupControl), new PropertyMetadata(default(GuidStructureItem), GroupDataChangedCallback));
  90. public void GeneratePreviewImage()
  91. {
  92. var layers = LayersViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.GetGroupLayers(GroupData);
  93. if (layers.Count > 0)
  94. {
  95. PreviewImage = BitmapUtils.GeneratePreviewBitmap(layers, 25, 25, true);
  96. }
  97. }
  98. private static void GroupDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  99. {
  100. ((LayerGroupControl)d).GeneratePreviewImage();
  101. }
  102. public WriteableBitmap PreviewImage
  103. {
  104. get { return (WriteableBitmap)GetValue(PreviewImageProperty); }
  105. set { SetValue(PreviewImageProperty, value); }
  106. }
  107. // Using a DependencyProperty as the backing store for PreviewImage. This enables animation, styling, binding, etc...
  108. public static readonly DependencyProperty PreviewImageProperty =
  109. DependencyProperty.Register("PreviewImage", typeof(WriteableBitmap), typeof(LayerGroupControl), new PropertyMetadata(default(WriteableBitmap)));
  110. public LayerGroupControl()
  111. {
  112. InitializeComponent();
  113. }
  114. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  115. {
  116. GeneratePreviewImage();
  117. }
  118. private void Grid_DragEnter(object sender, DragEventArgs e)
  119. {
  120. Grid item = sender as Grid;
  121. item.Background = LayerItem.HighlightColor;
  122. }
  123. private void Grid_DragLeave(object sender, DragEventArgs e)
  124. {
  125. Grid grid = (Grid)sender;
  126. LayerItem.RemoveDragEffect(grid);
  127. }
  128. private void HandleDrop(IDataObject dataObj, Grid grid, bool above)
  129. {
  130. Guid referenceLayer = above ? GroupData.EndLayerGuid : GroupData.StartLayerGuid;
  131. LayerItem.RemoveDragEffect(grid);
  132. if (dataObj.GetDataPresent(LayerContainerDataName))
  133. {
  134. HandleLayerDrop(dataObj, above, referenceLayer);
  135. }
  136. if (dataObj.GetDataPresent(LayerGroupControlDataName))
  137. {
  138. HandleGroupControlDrop(dataObj, referenceLayer, above);
  139. }
  140. }
  141. private void HandleLayerDrop(IDataObject dataObj, bool above, Guid referenceLayer)
  142. {
  143. var data = (LayerStructureItemContainer)dataObj.GetData(LayerContainerDataName);
  144. Guid group = data.Layer.LayerGuid;
  145. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.MoveLayerInStructure(group, referenceLayer, above);
  146. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.AssignParent(group, GroupData?.Parent?.GroupGuid);
  147. }
  148. private void HandleGroupControlDrop(IDataObject dataObj, Guid referenceLayer, bool above)
  149. {
  150. var data = (LayerGroupControl)dataObj.GetData(LayerGroupControlDataName);
  151. var document = data.LayersViewModel.Owner.BitmapManager.ActiveDocument;
  152. Guid group = data.GroupGuid;
  153. if (group == GroupGuid || document.LayerStructure.IsChildOf(GroupData, data.GroupData))
  154. {
  155. return;
  156. }
  157. int modifier = above ? 1 : 0;
  158. Layer layer = document.Layers.First(x => x.LayerGuid == referenceLayer);
  159. int indexOfReferenceLayer = Math.Clamp(document.Layers.IndexOf(layer) + modifier, 0, document.Layers.Count);
  160. MoveGroupWithTempLayer(above, document, group, indexOfReferenceLayer);
  161. }
  162. private void MoveGroupWithTempLayer(bool above, Models.DataHolders.Document document, Guid group, int indexOfReferenceLayer)
  163. {
  164. // The trick here is to insert a temp layer, assign group to it, then delete it.
  165. Layer tempLayer = new("_temp");
  166. document.Layers.Insert(indexOfReferenceLayer, tempLayer);
  167. document.LayerStructure.AssignParent(tempLayer.LayerGuid, GroupData?.Parent?.GroupGuid);
  168. document.MoveGroupInStructure(group, tempLayer.LayerGuid, above);
  169. document.LayerStructure.AssignParent(tempLayer.LayerGuid, null);
  170. document.RemoveLayer(tempLayer, false);
  171. }
  172. private void Grid_Drop_Top(object sender, DragEventArgs e)
  173. {
  174. HandleDrop(e.Data, (Grid)sender, true);
  175. }
  176. private void Grid_Drop_Bottom(object sender, DragEventArgs e)
  177. {
  178. HandleDrop(e.Data, (Grid)sender, false);
  179. }
  180. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  181. {
  182. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  183. doc.SetMainActiveLayer(doc.Layers.IndexOf(doc.Layers.First(x => x.LayerGuid == GroupData.EndLayerGuid)));
  184. }
  185. }
  186. }