LayersManager.xaml.cs 9.2 KB

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