LayerGroupControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using PixiEditor.Models.ImageManipulation;
  2. using PixiEditor.Models.Layers;
  3. using PixiEditor.Models.Undo;
  4. using PixiEditor.ViewModels.SubViewModels.Main;
  5. using System;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. namespace PixiEditor.Views.UserControls.Layers
  12. {
  13. /// <summary>
  14. /// Interaction logic for LayerFolder.xaml.
  15. /// </summary>
  16. public partial class LayerGroupControl : UserControl
  17. {
  18. public Guid GroupGuid
  19. {
  20. get { return (Guid)GetValue(GroupGuidProperty); }
  21. set { SetValue(GroupGuidProperty, value); }
  22. }
  23. public static readonly DependencyProperty GroupGuidProperty =
  24. DependencyProperty.Register("GroupGuid", typeof(Guid), typeof(LayerGroupControl), new PropertyMetadata(Guid.NewGuid()));
  25. public LayersViewModel LayersViewModel
  26. {
  27. get { return (LayersViewModel)GetValue(LayersViewModelProperty); }
  28. set { SetValue(LayersViewModelProperty, value); }
  29. }
  30. public static readonly DependencyProperty LayersViewModelProperty =
  31. DependencyProperty.Register("LayersViewModel", typeof(LayersViewModel), typeof(LayerGroupControl), new PropertyMetadata(default(LayersViewModel), LayersViewModelCallback));
  32. public bool IsVisibleUndoTriggerable
  33. {
  34. get { return (bool)GetValue(IsVisibleUndoTriggerableProperty); }
  35. set { SetValue(IsVisibleUndoTriggerableProperty, value); }
  36. }
  37. public static readonly DependencyProperty IsVisibleUndoTriggerableProperty =
  38. DependencyProperty.Register("IsVisibleUndoTriggerable", typeof(bool), typeof(LayerGroupControl), new PropertyMetadata(true));
  39. public float GroupOpacity
  40. {
  41. get { return (float)GetValue(GroupOpacityProperty); }
  42. set { SetValue(GroupOpacityProperty, value); }
  43. }
  44. public static readonly DependencyProperty GroupOpacityProperty =
  45. DependencyProperty.Register("GroupOpacity", typeof(float), typeof(LayerGroupControl), new PropertyMetadata(1f));
  46. public static string LayerGroupControlDataName = typeof(LayerGroupControl).FullName;
  47. public static string LayerContainerDataName = typeof(LayerStructureItemContainer).FullName;
  48. private static void LayersViewModelCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. {
  50. LayerGroupControl control = (LayerGroupControl)d;
  51. if (e.OldValue is LayersViewModel oldVm && oldVm != e.NewValue)
  52. {
  53. oldVm.Owner.BitmapManager.StopUsingTool -= control.MouseController_StoppedRecordingChanges;
  54. }
  55. if (e.NewValue is LayersViewModel vm)
  56. {
  57. vm.Owner.BitmapManager.StopUsingTool += control.MouseController_StoppedRecordingChanges;
  58. }
  59. }
  60. public string GroupName
  61. {
  62. get { return (string)GetValue(GroupNameProperty); }
  63. set { SetValue(GroupNameProperty, value); }
  64. }
  65. public static readonly DependencyProperty GroupNameProperty =
  66. DependencyProperty.Register("GroupName", typeof(string), typeof(LayerGroupControl), new PropertyMetadata(default(string)));
  67. public GuidStructureItem GroupData
  68. {
  69. get { return (GuidStructureItem)GetValue(GroupDataProperty); }
  70. set { SetValue(GroupDataProperty, value); }
  71. }
  72. public static readonly DependencyProperty GroupDataProperty =
  73. DependencyProperty.Register("GroupData", typeof(GuidStructureItem), typeof(LayerGroupControl), new PropertyMetadata(default(GuidStructureItem), GroupDataChangedCallback));
  74. public void GeneratePreviewImage()
  75. {
  76. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  77. var layers = doc.LayerStructure.GetGroupLayers(GroupData);
  78. if (layers.Count > 0)
  79. {
  80. PreviewImage = BitmapUtils.GeneratePreviewBitmap(layers, doc.Width, doc.Height, 25, 25);
  81. }
  82. }
  83. private static void GroupDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  84. {
  85. LayerGroupControl control = (LayerGroupControl)d;
  86. control.GeneratePreviewImage();
  87. foreach (var layer in control.LayersViewModel.Owner.BitmapManager.ActiveDocument.Layers)
  88. {
  89. layer.IsVisible = layer.IsVisible;
  90. }
  91. }
  92. public WriteableBitmap PreviewImage
  93. {
  94. get { return (WriteableBitmap)GetValue(PreviewImageProperty); }
  95. set { SetValue(PreviewImageProperty, value); }
  96. }
  97. public static readonly DependencyProperty PreviewImageProperty =
  98. DependencyProperty.Register("PreviewImage", typeof(WriteableBitmap), typeof(LayerGroupControl), new PropertyMetadata(default(WriteableBitmap)));
  99. public LayerGroupControl()
  100. {
  101. InitializeComponent();
  102. }
  103. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  104. {
  105. GeneratePreviewImage();
  106. }
  107. private void Grid_DragEnter(object sender, DragEventArgs e)
  108. {
  109. Grid item = sender as Grid;
  110. item.Background = LayerItem.HighlightColor;
  111. }
  112. private void Grid_CenterEnter(object sender, DragEventArgs e)
  113. {
  114. centerGrid.Background = LayerItem.HighlightColor;
  115. }
  116. private void Grid_DragLeave(object sender, DragEventArgs e)
  117. {
  118. Grid grid = (Grid)sender;
  119. LayerItem.RemoveDragEffect(grid);
  120. }
  121. private void Grid_CenterLeave(object sender, DragEventArgs e)
  122. {
  123. LayerItem.RemoveDragEffect(centerGrid);
  124. }
  125. private void HandleDrop(IDataObject dataObj, Grid grid, bool above)
  126. {
  127. Guid referenceLayer = above ? GroupData.EndLayerGuid : GroupData.StartLayerGuid;
  128. LayerItem.RemoveDragEffect(grid);
  129. if (dataObj.GetDataPresent(LayerContainerDataName))
  130. {
  131. HandleLayerDrop(dataObj, above, referenceLayer, false);
  132. }
  133. if (dataObj.GetDataPresent(LayerGroupControlDataName))
  134. {
  135. HandleGroupControlDrop(dataObj, referenceLayer, above, false);
  136. }
  137. }
  138. private void HandleLayerDrop(IDataObject dataObj, bool above, Guid referenceLayer, bool putItInside) // step brother
  139. {
  140. var data = (LayerStructureItemContainer)dataObj.GetData(LayerContainerDataName);
  141. Guid group = data.Layer.GuidValue;
  142. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.MoveLayerInStructure(group, referenceLayer, above);
  143. Guid? refGuid = putItInside ? GroupData?.GroupGuid : GroupData?.Parent?.GroupGuid;
  144. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.AssignParent(group, refGuid);
  145. }
  146. private void HandleGroupControlDrop(IDataObject dataObj, Guid referenceLayer, bool above, bool putItInside) // daddy
  147. {
  148. var data = (LayerGroupControl)dataObj.GetData(LayerGroupControlDataName);
  149. var document = data.LayersViewModel.Owner.BitmapManager.ActiveDocument;
  150. Guid group = data.GroupGuid;
  151. if (group == GroupGuid || document.LayerStructure.IsChildOf(GroupData, data.GroupData))
  152. {
  153. return;
  154. }
  155. int modifier = above ? 1 : 0;
  156. Layer layer = document.Layers.First(x => x.GuidValue == referenceLayer);
  157. int indexOfReferenceLayer = Math.Clamp(document.Layers.IndexOf(layer) + modifier, 0, document.Layers.Count);
  158. MoveGroupWithTempLayer(above, document, group, indexOfReferenceLayer, putItInside);
  159. }
  160. private void MoveGroupWithTempLayer(bool above, Models.DataHolders.Document document, Guid group, int indexOfReferenceLayer, bool putItInside) // ¯\_(ツ)_/¯
  161. {
  162. // The trick here is to insert a temp layer, assign group to it, then delete it.
  163. Layer tempLayer = new("_temp");
  164. document.Layers.Insert(indexOfReferenceLayer, tempLayer);
  165. Guid? refGuid = putItInside ? GroupData?.GroupGuid : GroupData?.Parent?.GroupGuid;
  166. document.LayerStructure.AssignParent(tempLayer.GuidValue, refGuid);
  167. document.MoveGroupInStructure(group, tempLayer.GuidValue, above);
  168. document.LayerStructure.AssignParent(tempLayer.GuidValue, null);
  169. document.RemoveLayer(tempLayer, false);
  170. }
  171. private void HandleDropInside(IDataObject dataObj, Grid grid)
  172. {
  173. Guid referenceLayer = GroupData.EndLayerGuid;
  174. LayerItem.RemoveDragEffect(grid);
  175. if (dataObj.GetDataPresent(LayerContainerDataName))
  176. {
  177. HandleLayerDrop(dataObj, true, referenceLayer, true);
  178. }
  179. if (dataObj.GetDataPresent(LayerGroupControlDataName))
  180. {
  181. HandleGroupControlDrop(dataObj, referenceLayer, true, true);
  182. }
  183. }
  184. private void Grid_Drop_Top(object sender, DragEventArgs e)
  185. {
  186. HandleDrop(e.Data, (Grid)sender, true);
  187. }
  188. private void Grid_Drop_Center(object sender, DragEventArgs e)
  189. {
  190. HandleDropInside(e.Data, (Grid)sender);
  191. LayerItem.RemoveDragEffect(centerGrid);
  192. }
  193. private void Grid_Drop_Bottom(object sender, DragEventArgs e)
  194. {
  195. HandleDrop(e.Data, (Grid)sender, false);
  196. }
  197. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  198. {
  199. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  200. var layer = doc.Layers.First(x => x.GuidValue == GroupData.EndLayerGuid);
  201. if (doc.ActiveLayerGuid != layer.GuidValue)
  202. {
  203. doc.SetMainActiveLayer(doc.Layers.IndexOf(layer));
  204. }
  205. }
  206. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  207. {
  208. HandleCheckboxChange(((CheckBox)e.OriginalSource).IsChecked.Value);
  209. }
  210. private void HandleCheckboxChange(bool value)
  211. {
  212. if (LayersViewModel?.Owner?.BitmapManager?.ActiveDocument != null)
  213. {
  214. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  215. IsVisibleUndoTriggerable = value;
  216. var processArgs = new object[] { GroupGuid, value };
  217. var reverseProcessArgs = new object[] { GroupGuid, !value };
  218. ChangeGroupVisibilityProcess(processArgs);
  219. doc.UndoManager.AddUndoChange(
  220. new Change(
  221. ChangeGroupVisibilityProcess,
  222. reverseProcessArgs,
  223. ChangeGroupVisibilityProcess,
  224. processArgs,
  225. $"Change {GroupName} visibility"), false);
  226. }
  227. }
  228. private void ChangeGroupVisibilityProcess(object[] args)
  229. {
  230. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  231. if (args.Length == 2 &&
  232. args[0] is Guid groupGuid &&
  233. args[1] is bool value
  234. && doc != null)
  235. {
  236. var group = doc.LayerStructure.GetGroupByGuid(groupGuid);
  237. group.IsVisible = value;
  238. var layers = doc.LayerStructure.GetGroupLayers(group);
  239. foreach (var layer in layers)
  240. {
  241. layer.IsVisible = layer.IsVisible;
  242. }
  243. IsVisibleUndoTriggerable = value;
  244. }
  245. }
  246. private void GroupControl_DragEnter(object sender, DragEventArgs e)
  247. {
  248. middleDropGrid.Visibility = Visibility.Visible;
  249. }
  250. private void GroupControl_DragLeave(object sender, DragEventArgs e)
  251. {
  252. middleDropGrid.Visibility = Visibility.Collapsed;
  253. }
  254. }
  255. }