LayersManager.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using PixiEditor.Models.Controllers;
  7. using PixiEditor.Models.Layers;
  8. using PixiEditor.Models.Undo;
  9. using PixiEditor.ViewModels.SubViewModels.Main;
  10. namespace PixiEditor.Views.UserControls
  11. {
  12. /// <summary>
  13. /// Interaction logic for LayersManager.xaml.
  14. /// </summary>
  15. public partial class LayersManager : UserControl
  16. {
  17. public object SelectedItem
  18. {
  19. get { return (object)GetValue(SelectedItemProperty); }
  20. set { SetValue(SelectedItemProperty, value); }
  21. }
  22. // Using a DependencyProperty as the backing store for SelectedItem. This enables animation, styling, binding, etc...
  23. public static readonly DependencyProperty SelectedItemProperty =
  24. DependencyProperty.Register("SelectedItem", typeof(object), typeof(LayersManager), new PropertyMetadata(0));
  25. public ObservableCollection<object> LayerTreeRoot
  26. {
  27. get { return (ObservableCollection<object>)GetValue(LayerTreeRootProperty); }
  28. set { SetValue(LayerTreeRootProperty, value); }
  29. }
  30. // Using a DependencyProperty as the backing store for LayerTreeRoot. This enables animation, styling, binding, etc...
  31. public static readonly DependencyProperty LayerTreeRootProperty =
  32. DependencyProperty.Register(
  33. "LayerTreeRoot",
  34. typeof(ObservableCollection<object>),
  35. typeof(LayersManager),
  36. new PropertyMetadata(default(ObservableCollection<object>)));
  37. public LayersViewModel LayerCommandsViewModel
  38. {
  39. get { return (LayersViewModel)GetValue(LayerCommandsViewModelProperty); }
  40. set { SetValue(LayerCommandsViewModelProperty, value); }
  41. }
  42. // Using a DependencyProperty as the backing store for LayerCommandsViewModel. This enables animation, styling, binding, etc...
  43. public static readonly DependencyProperty LayerCommandsViewModelProperty =
  44. DependencyProperty.Register("LayerCommandsViewModel", typeof(LayersViewModel), typeof(LayersManager), new PropertyMetadata(default(LayersViewModel), ViewModelChanged));
  45. public bool OpacityInputEnabled
  46. {
  47. get { return (bool)GetValue(OpacityInputEnabledProperty); }
  48. set { SetValue(OpacityInputEnabledProperty, value); }
  49. }
  50. // Using a DependencyProperty as the backing store for OpacityInputEnabled. This enables animation, styling, binding, etc...
  51. public static readonly DependencyProperty OpacityInputEnabledProperty =
  52. DependencyProperty.Register("OpacityInputEnabled", typeof(bool), typeof(LayersManager), new PropertyMetadata(false));
  53. public LayersManager()
  54. {
  55. InitializeComponent();
  56. }
  57. private static void ViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. if (e.NewValue is LayersViewModel vm)
  60. {
  61. LayersManager manager = (LayersManager)d;
  62. vm.Owner.BitmapManager.AddPropertyChangedCallback(nameof(vm.Owner.BitmapManager.ActiveDocument), () =>
  63. {
  64. var doc = vm.Owner.BitmapManager.ActiveDocument;
  65. if (doc != null)
  66. {
  67. doc.AddPropertyChangedCallback(nameof(doc.ActiveLayer), () =>
  68. {
  69. manager.SelectedItem = doc.ActiveLayer;
  70. manager.SetInputOpacity(manager.SelectedItem);
  71. });
  72. }
  73. });
  74. }
  75. }
  76. private void SetInputOpacity(object item)
  77. {
  78. if (item is Layer layer)
  79. {
  80. numberInput.Value = layer.Opacity * 100f;
  81. }
  82. else if (item is LayerGroup group)
  83. {
  84. numberInput.Value = group.StructureData.Opacity * 100f;
  85. }
  86. else if (item is LayerGroupControl groupControl)
  87. {
  88. numberInput.Value = groupControl.GroupData.Opacity * 100f;
  89. }
  90. }
  91. private void LayerStructureItemContainer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  92. {
  93. if (sender is LayerStructureItemContainer container && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
  94. {
  95. DragDrop.DoDragDrop(container, container, DragDropEffects.Move);
  96. }
  97. }
  98. private void HandleGroupOpacityChange(GuidStructureItem group, float value)
  99. {
  100. if (LayerCommandsViewModel.Owner?.BitmapManager?.ActiveDocument != null)
  101. {
  102. var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
  103. var processArgs = new object[] { group.GroupGuid, value };
  104. var reverseProcessArgs = new object[] { group.GroupGuid, group.Opacity };
  105. ChangeGroupOpacityProcess(processArgs);
  106. LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.ExpandParentGroups(group);
  107. doc.UndoManager.AddUndoChange(
  108. new Change(
  109. ChangeGroupOpacityProcess,
  110. reverseProcessArgs,
  111. ChangeGroupOpacityProcess,
  112. processArgs,
  113. $"Change {group.Name} opacity"), false);
  114. }
  115. }
  116. private void ChangeGroupOpacityProcess(object[] processArgs)
  117. {
  118. if (processArgs.Length > 0 && processArgs[0] is Guid groupGuid && processArgs[1] is float opacity)
  119. {
  120. var structure = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure;
  121. var group = structure.GetGroupByGuid(groupGuid);
  122. group.Opacity = opacity;
  123. var layers = structure.GetGroupLayers(group);
  124. layers.ForEach(x => x.Opacity = x.Opacity); // This might seems stupid, but it raises property changed, without setting any value. This is used to trigger converters that use group opacity
  125. numberInput.Value = opacity * 100;
  126. }
  127. }
  128. private void LayerGroup_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  129. {
  130. if (sender is LayerGroupControl container && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
  131. {
  132. DragDrop.DoDragDrop(container, container, DragDropEffects.Move);
  133. }
  134. }
  135. private void NumberInput_LostFocus(object sender, RoutedEventArgs e)
  136. {
  137. float val = numberInput.Value / 100f;
  138. object item = SelectedItem;
  139. if (item is Layer layer)
  140. {
  141. float oldOpacity = layer.Opacity;
  142. var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
  143. layer.OpacityUndoTriggerable = val;
  144. doc.LayerStructure.ExpandParentGroups(layer.LayerGuid);
  145. doc.RaisePropertyChange(nameof(doc.LayerStructure));
  146. UndoManager undoManager = doc.UndoManager;
  147. undoManager.AddUndoChange(
  148. new Change(
  149. UpdateNumberInputLayerOpacityProcess,
  150. new object[] { oldOpacity },
  151. UpdateNumberInputLayerOpacityProcess,
  152. new object[] { val }));
  153. undoManager.SquashUndoChanges(2);
  154. }
  155. else if (item is LayerGroup group)
  156. {
  157. HandleGroupOpacityChange(group.StructureData, val);
  158. }
  159. else if(item is LayerGroupControl groupControl)
  160. {
  161. HandleGroupOpacityChange(groupControl.GroupData, val);
  162. }
  163. }
  164. private void UpdateNumberInputLayerOpacityProcess(object[] args)
  165. {
  166. if (args.Length > 0 && args[0] is float opacity)
  167. {
  168. numberInput.Value = opacity * 100;
  169. }
  170. }
  171. private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  172. {
  173. SetInputOpacity(SelectedItem);
  174. }
  175. private void TreeView_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  176. {
  177. if (e.ClickCount > 1)
  178. {
  179. if (sender is TreeView treeView && treeView.SelectedItem is LayerGroup group)
  180. {
  181. group.StructureData.IsRenaming = true;
  182. e.Handled = true;
  183. }
  184. }
  185. }
  186. private void Grid_Drop(object sender, DragEventArgs e)
  187. {
  188. dropBorder.BorderBrush = Brushes.Transparent;
  189. if (e.Data.GetDataPresent(LayerGroupControl.LayerContainerDataName))
  190. {
  191. HandleLayerDrop(e.Data);
  192. }
  193. if (e.Data.GetDataPresent(LayerGroupControl.LayerGroupControlDataName))
  194. {
  195. HandleGroupControlDrop(e.Data);
  196. }
  197. }
  198. private void HandleLayerDrop(IDataObject data)
  199. {
  200. var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
  201. if (doc.Layers.Count == 0) return;
  202. var layerContainer = (LayerStructureItemContainer)data.GetData(LayerGroupControl.LayerContainerDataName);
  203. var refLayer = doc.Layers[0].LayerGuid;
  204. doc.MoveLayerInStructure(layerContainer.Layer.LayerGuid, refLayer);
  205. doc.LayerStructure.AssignParent(layerContainer.Layer.LayerGuid, null);
  206. }
  207. private void HandleGroupControlDrop(IDataObject data)
  208. {
  209. var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
  210. var groupContainer = (LayerGroupControl)data.GetData(LayerGroupControl.LayerGroupControlDataName);
  211. doc.LayerStructure.MoveGroup(groupContainer.GroupGuid, 0);
  212. }
  213. private void Grid_DragEnter(object sender, DragEventArgs e)
  214. {
  215. ((Border)sender).BorderBrush = LayerItem.HighlightColor;
  216. }
  217. private void Grid_DragLeave(object sender, DragEventArgs e)
  218. {
  219. ((Border)sender).BorderBrush = Brushes.Transparent;
  220. }
  221. private void SelectActiveItem(object sender, System.Windows.Input.MouseButtonEventArgs e)
  222. {
  223. SelectedItem = sender;
  224. }
  225. }
  226. }