LayersManager.xaml.cs 9.9 KB

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