LayerGroupControl.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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));
  45. private static void LayersViewModelCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  46. {
  47. LayerGroupControl control = (LayerGroupControl)d;
  48. if(e.OldValue is LayersViewModel oldVm && oldVm != e.NewValue)
  49. {
  50. oldVm.Owner.BitmapManager.MouseController.StoppedRecordingChanges -= control.MouseController_StoppedRecordingChanges;
  51. }
  52. if(e.NewValue is LayersViewModel vm)
  53. {
  54. vm.Owner.BitmapManager.MouseController.StoppedRecordingChanges += control.MouseController_StoppedRecordingChanges;
  55. }
  56. }
  57. public string GroupName
  58. {
  59. get { return (string)GetValue(GroupNameProperty); }
  60. set { SetValue(GroupNameProperty, value); }
  61. }
  62. // Using a DependencyProperty as the backing store for FolderName. This enables animation, styling, binding, etc...
  63. public static readonly DependencyProperty GroupNameProperty =
  64. DependencyProperty.Register("GroupName", typeof(string), typeof(LayerGroupControl), new PropertyMetadata(default(string)));
  65. public GuidStructureItem GroupData
  66. {
  67. get { return (GuidStructureItem)GetValue(GroupDataProperty); }
  68. set { SetValue(GroupDataProperty, value); }
  69. }
  70. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  71. public static readonly DependencyProperty GroupDataProperty =
  72. DependencyProperty.Register("GroupData", typeof(GuidStructureItem), typeof(LayerGroupControl), new PropertyMetadata(default(GuidStructureItem), GroupDataChangedCallback));
  73. public void GeneratePreviewImage()
  74. {
  75. var layers = LayersViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.GetGroupLayers(GroupData);
  76. if (layers.Count > 0)
  77. {
  78. PreviewImage = BitmapUtils.GeneratePreviewBitmap(layers, 25, 25, true);
  79. }
  80. }
  81. private static void GroupDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  82. {
  83. ((LayerGroupControl)d).GeneratePreviewImage();
  84. }
  85. public WriteableBitmap PreviewImage
  86. {
  87. get { return (WriteableBitmap)GetValue(PreviewImageProperty); }
  88. set { SetValue(PreviewImageProperty, value); }
  89. }
  90. // Using a DependencyProperty as the backing store for PreviewImage. This enables animation, styling, binding, etc...
  91. public static readonly DependencyProperty PreviewImageProperty =
  92. DependencyProperty.Register("PreviewImage", typeof(WriteableBitmap), typeof(LayerGroupControl), new PropertyMetadata(default(WriteableBitmap)));
  93. public LayerGroupControl()
  94. {
  95. InitializeComponent();
  96. }
  97. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  98. {
  99. GeneratePreviewImage();
  100. }
  101. private void Grid_DragEnter(object sender, DragEventArgs e)
  102. {
  103. Grid item = sender as Grid;
  104. item.Background = LayerItem.HighlightColor;
  105. }
  106. private void Grid_DragLeave(object sender, DragEventArgs e)
  107. {
  108. Grid grid = (Grid)sender;
  109. LayerItem.RemoveDragEffect(grid);
  110. }
  111. private void HandleDrop(IDataObject dataObj, Grid grid, bool above)
  112. {
  113. Guid referenceLayer = above ? GroupData.EndLayerGuid : GroupData.StartLayerGuid;
  114. LayerItem.RemoveDragEffect(grid);
  115. if (dataObj.GetDataPresent(LayerContainerDataName))
  116. {
  117. HandleLayerDrop(dataObj, above, referenceLayer);
  118. }
  119. if (dataObj.GetDataPresent(LayerGroupControlDataName))
  120. {
  121. HandleGroupControlDrop(dataObj, referenceLayer, above);
  122. }
  123. }
  124. private void HandleLayerDrop(IDataObject dataObj, bool above, Guid referenceLayer)
  125. {
  126. var data = (LayerStructureItemContainer)dataObj.GetData(LayerContainerDataName);
  127. Guid group = data.Layer.LayerGuid;
  128. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.MoveLayerInStructure(group, referenceLayer, above);
  129. data.LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.AssignParent(group, GroupData?.Parent?.GroupGuid);
  130. }
  131. private void HandleGroupControlDrop(IDataObject dataObj, Guid referenceLayer, bool above)
  132. {
  133. var data = (LayerGroupControl)dataObj.GetData(LayerGroupControlDataName);
  134. var document = data.LayersViewModel.Owner.BitmapManager.ActiveDocument;
  135. Guid group = data.GroupGuid;
  136. if (group == GroupGuid || document.LayerStructure.IsChildOf(GroupData, data.GroupData))
  137. {
  138. return;
  139. }
  140. int modifier = above ? 1 : 0;
  141. Layer layer = document.Layers.First(x => x.LayerGuid == referenceLayer);
  142. int indexOfReferenceLayer = Math.Clamp(document.Layers.IndexOf(layer) + modifier, 0, document.Layers.Count);
  143. MoveGroupWithTempLayer(above, document, group, indexOfReferenceLayer);
  144. }
  145. private void MoveGroupWithTempLayer(bool above, Models.DataHolders.Document document, Guid group, int indexOfReferenceLayer)
  146. {
  147. // The trick here is to insert a temp layer, assign group to it, then delete it.
  148. Layer tempLayer = new("_temp");
  149. document.Layers.Insert(indexOfReferenceLayer, tempLayer);
  150. document.LayerStructure.AssignParent(tempLayer.LayerGuid, GroupData?.Parent?.GroupGuid);
  151. document.MoveGroupInStructure(group, tempLayer.LayerGuid, above);
  152. document.LayerStructure.AssignParent(tempLayer.LayerGuid, null);
  153. document.RemoveLayer(tempLayer, false);
  154. }
  155. private void Grid_Drop_Top(object sender, DragEventArgs e)
  156. {
  157. HandleDrop(e.Data, (Grid)sender, true);
  158. }
  159. private void Grid_Drop_Bottom(object sender, DragEventArgs e)
  160. {
  161. HandleDrop(e.Data, (Grid)sender, false);
  162. }
  163. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  164. {
  165. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  166. doc.SetMainActiveLayer(doc.Layers.IndexOf(doc.Layers.First(x => x.LayerGuid == GroupData.EndLayerGuid)));
  167. }
  168. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  169. {
  170. HandleCheckboxChange(true);
  171. }
  172. private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
  173. {
  174. HandleCheckboxChange(false);
  175. }
  176. private void HandleCheckboxChange(bool value)
  177. {
  178. if (LayersViewModel?.Owner?.BitmapManager?.ActiveDocument != null)
  179. {
  180. var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
  181. doc.LayerStructure.GetGroupByGuid(GroupGuid).IsVisible = value;
  182. var layers = doc.LayerStructure.GetGroupLayers(GroupData);
  183. foreach (var layer in layers)
  184. {
  185. layer.RaisePropertyChange(nameof(layer.IsVisible));
  186. }
  187. doc.UndoManager.AddUndoChange(
  188. new Change(
  189. nameof(IsVisibleUndoTriggerable),
  190. !value,
  191. value,
  192. $"Change {GroupName} visibility",
  193. this), true) //wip, doesn't work
  194. }
  195. }
  196. }
  197. }