LayersManager.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using PixiEditor.Models.Controllers;
  6. using PixiEditor.Models.Layers;
  7. using PixiEditor.Models.Undo;
  8. using PixiEditor.ViewModels.SubViewModels.Main;
  9. namespace PixiEditor.Views.UserControls
  10. {
  11. /// <summary>
  12. /// Interaction logic for LayersManager.xaml.
  13. /// </summary>
  14. public partial class LayersManager : UserControl
  15. {
  16. private object cachedItem;
  17. public ObservableCollection<object> LayerTreeRoot
  18. {
  19. get { return (ObservableCollection<object>)GetValue(LayerTreeRootProperty); }
  20. set { SetValue(LayerTreeRootProperty, value); }
  21. }
  22. // Using a DependencyProperty as the backing store for LayerTreeRoot. This enables animation, styling, binding, etc...
  23. public static readonly DependencyProperty LayerTreeRootProperty =
  24. DependencyProperty.Register(
  25. "LayerTreeRoot",
  26. typeof(ObservableCollection<object>),
  27. typeof(LayersManager),
  28. new PropertyMetadata(default(ObservableCollection<object>), ItemsChanged));
  29. public LayersViewModel LayerCommandsViewModel
  30. {
  31. get { return (LayersViewModel)GetValue(LayerCommandsViewModelProperty); }
  32. set { SetValue(LayerCommandsViewModelProperty, value); }
  33. }
  34. // Using a DependencyProperty as the backing store for LayerCommandsViewModel. This enables animation, styling, binding, etc...
  35. public static readonly DependencyProperty LayerCommandsViewModelProperty =
  36. DependencyProperty.Register("LayerCommandsViewModel", typeof(LayersViewModel), typeof(LayersManager), new PropertyMetadata(default(LayersViewModel)));
  37. public bool OpacityInputEnabled
  38. {
  39. get { return (bool)GetValue(OpacityInputEnabledProperty); }
  40. set { SetValue(OpacityInputEnabledProperty, value); }
  41. }
  42. // Using a DependencyProperty as the backing store for OpacityInputEnabled. This enables animation, styling, binding, etc...
  43. public static readonly DependencyProperty OpacityInputEnabledProperty =
  44. DependencyProperty.Register("OpacityInputEnabled", typeof(bool), typeof(LayersManager), new PropertyMetadata(false));
  45. public LayersManager()
  46. {
  47. InitializeComponent();
  48. }
  49. private void LayerStructureItemContainer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  50. {
  51. if (sender is LayerStructureItemContainer container && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
  52. {
  53. DragDrop.DoDragDrop(container, container, DragDropEffects.Move);
  54. }
  55. }
  56. private static void ItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. var items = (ObservableCollection<object>)e.NewValue;
  59. LayersManager manager = (LayersManager)d;
  60. var numberInput = manager.numberInput;
  61. object item = manager.treeView.SelectedItem;
  62. if (items != null && items.Count > 0 && (e.OldValue == null || ((ObservableCollection<object>)e.OldValue).Count == 0))
  63. {
  64. item = items[0];
  65. manager.cachedItem = item;
  66. }
  67. SetInputOpacity(item, numberInput);
  68. }
  69. private static void SetInputOpacity(object item, NumberInput numberInput)
  70. {
  71. if (item is Layer layer)
  72. {
  73. numberInput.Value = layer.Opacity * 100f;
  74. }
  75. else if (item is LayerGroup group)
  76. {
  77. numberInput.Value = group.StructureData.Opacity * 100f;
  78. }
  79. }
  80. private void HandleGroupOpacityChange(LayerGroup group, float value)
  81. {
  82. if (LayerCommandsViewModel.Owner?.BitmapManager?.ActiveDocument != null)
  83. {
  84. var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
  85. var processArgs = new object[] { group.StructureData.GroupGuid, value };
  86. var reverseProcessArgs = new object[] { group.StructureData.GroupGuid, group.StructureData.Opacity };
  87. ChangeGroupOpacityProcess(processArgs);
  88. doc.UndoManager.AddUndoChange(
  89. new Change(
  90. ChangeGroupOpacityProcess,
  91. reverseProcessArgs,
  92. ChangeGroupOpacityProcess,
  93. processArgs,
  94. $"Change {group.Name} opacity"), false);
  95. }
  96. }
  97. private void ChangeGroupOpacityProcess(object[] processArgs)
  98. {
  99. if (processArgs.Length > 0 && processArgs[0] is Guid groupGuid && processArgs[1] is float opacity)
  100. {
  101. var structure = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure;
  102. var group = structure.GetGroupByGuid(groupGuid);
  103. group.Opacity = opacity;
  104. var layers = structure.GetGroupLayers(group);
  105. 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
  106. numberInput.Value = opacity * 100;
  107. }
  108. }
  109. private void LayerGroup_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  110. {
  111. if (sender is LayerGroupControl container && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
  112. {
  113. DragDrop.DoDragDrop(container, container, DragDropEffects.Move);
  114. }
  115. }
  116. private void NumberInput_LostFocus(object sender, RoutedEventArgs e)
  117. {
  118. float val = numberInput.Value / 100f;
  119. object item = treeView.SelectedItem;
  120. if (item == null && cachedItem != null)
  121. {
  122. item = cachedItem;
  123. }
  124. if (item is Layer layer)
  125. {
  126. float oldOpacity = layer.Opacity;
  127. layer.OpacityUndoTriggerable = val;
  128. UndoManager undoManager = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.UndoManager;
  129. undoManager.AddUndoChange(
  130. new Change(
  131. UpdateNumberInputLayerOpacityProcess,
  132. new object[] { oldOpacity },
  133. UpdateNumberInputLayerOpacityProcess,
  134. new object[] { val }));
  135. undoManager.SquashUndoChanges(2);
  136. }
  137. else if(item is LayerGroup group)
  138. {
  139. HandleGroupOpacityChange(group, val);
  140. }
  141. }
  142. private void UpdateNumberInputLayerOpacityProcess(object[] args)
  143. {
  144. if(args.Length > 0 && args[0] is float opacity)
  145. {
  146. numberInput.Value = opacity * 100;
  147. }
  148. }
  149. private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  150. {
  151. SetInputOpacity(treeView.SelectedItem, numberInput);
  152. }
  153. private void TreeView_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  154. {
  155. if (e.ClickCount > 1)
  156. {
  157. if(sender is TreeView treeView && treeView.SelectedItem is LayerGroup group)
  158. {
  159. group.StructureData.IsRenaming = true;
  160. e.Handled = true;
  161. }
  162. }
  163. }
  164. }
  165. }